import React, { Component } from 'react'

const MsgContext = React.createContext()
const ThemeContext = React.createContext()

class Son extends Component {
  // static contextType = MsgContext
  render() {
    return (
      <div>
        <h3>儿子</h3>
        {/* {this.context} */}
        <ThemeContext.Consumer>
          {(theme) => {
            return (
              <MsgContext.Consumer>
                {(msg) => {
                  return (
                    <span>
                      theme:{theme}---msg:{msg}
                    </span>
                  )
                }}
              </MsgContext.Consumer>
            )
          }}
        </ThemeContext.Consumer>
      </div>
    )
  }
}

// Son.contextType = MsgContext

class Parent extends Component {
  render() {
    return (
      <div>
        <h2>爸爸</h2>
        <Son />
      </div>
    )
  }
}

export default class App extends Component {
  state = {
    msg: '爷爷给的糖',
    theme: 'red'
  }
  render() {
    return (
      <div>
        <h1>爷爷</h1>
        {/* value 值是固定写法不要去修改 */}
        <ThemeContext.Provider value={this.state.theme}>
          <MsgContext.Provider value={this.state.msg}>
            <Parent />
          </MsgContext.Provider>
        </ThemeContext.Provider>
      </div>
    )
  }
}
